package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/consts"
	"api-gateway/handler/request"
	"api-gateway/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func Longin(c *gin.Context) {
	var param request.Login
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}

	r, err := config.UserCline.GetUsernameByUsername(c, &__.GetUsernameByUsernameReq{
		Username: param.Username,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "用户不存在",
		})
		return
	}
	if r.UserId == 0 {
		_, err = config.UserCline.CreateUser(c, &__.CreateUserReq{
			Username: param.Username,
		})
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"mes":  "用户自动注册",
		})
		return
	}
	if r.Password != param.Password {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "密码错误",
		})
		return
	}
	token := pkg.Token(uint(r.UserId), time.Hour*2, consts.JWT_KEY)
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 200,
		"mes":  "登陆成功",
		"data": gin.H{
			"id":    r.UserId,
			"token": token,
		},
	})
}

func CreateGoodShopwindow(c *gin.Context) {
	var param request.GoodShopwindows
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}
	baidu, _ := pkg.Baidu(param.Title)
	if !baidu {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "有敏感词",
		})
		return
	}
	if param.Title == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "橱窗商品标题不能为空",
		})
		return
	}
	if param.Price < 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "橱窗商品价格不能是负数",
		})
		return
	}

	r, err := config.UserCline.CreateGoodShopwindow(c, &__.CreateGoodShopwindowReq{
		UserId:           param.UserId,
		GoodShopwindowId: param.GoodShopwindowId,
		Title:            param.Title,
		Image:            param.Image,
		Price:            float32(param.Price),
		Sales:            param.Sales,
		Stock:            param.Stock,
		TypeId:           param.TypeId,
		GoodId:           param.GoodId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "橱窗商品添加失败" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "橱窗商品添加成功",
		"data": r.Id,
	})
	return
}

func Type(c *gin.Context) {
	var param request.Type
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}
	r, err := config.UserCline.CreateGoodShopwindowType(c, &__.CreateGoodShopwindowTypeReq{
		Name: param.Name,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "橱窗商品分类添加失败" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "橱窗商品分类添加成功",
		"data": r.Id,
	})
}

func TypeList(c *gin.Context) {
	var param request.TypeList
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}
	r, err := config.UserCline.GoodTypeList(c, &__.GoodTypeListReq{})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "橱窗商品分类+商品关联展示失败" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "橱窗商品分类+商品关联展示成功",
		"data": r,
	})
}

func GoodStatus(c *gin.Context) {
	var param request.GoodStatus
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}
	r, err := config.UserCline.CheckGoodStatus(c, &__.CheckGoodStatusReq{
		Id:     param.Id,
		Status: param.Status,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "商品同步失败" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "商品同步成功",
		"data": r.Id,
	})
}

func GoodwindowStatus(c *gin.Context) {
	var param request.GoodWindowStatus
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}
	r, err := config.UserCline.CheckGoodShopwindowStatus(c, &__.CheckGoodShopwindowStatusReq{
		Id:     param.Id,
		Status: param.Status,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "橱窗商品状态失败" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "橱窗商品状态审核成功",
		"data": r.Id,
	})
}

func OrderAdd(c *gin.Context) {
	var param request.OrderAdd
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "参数错误" + err.Error(),
		})
		return
	}
	r, err := config.UserCline.OrderAdd(c, &__.OrderAddReq{
		Title:  param.Title,
		UserId: param.UserId,
		GoodId: param.GoodId,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"mes":  "订单添加失败" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "订单添加成功",
		"data": r.Id,
	})
}
